//
//  BiBorrowManageViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-27.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

static CGRect frame;

#import "BiBorrowManageViewController.h"

@interface BiBorrowManageViewController ()<UITextViewDelegate>{
    
    __weak IBOutlet UIScrollView *_ScrollView;
}
@property (weak, nonatomic) IBOutlet UILabel *labBorrowCarPerson;
@property (weak, nonatomic) IBOutlet UILabel *labVIN;
@property (weak, nonatomic) IBOutlet UILabel *labDirverName;
@property (weak, nonatomic) IBOutlet UILabel *labStoreroomManerName;
@property (weak, nonatomic) IBOutlet UILabel *labDriverPreformance;
@property (weak, nonatomic) IBOutlet UILabel *labStoreroomManerPreformance;
@property (weak, nonatomic) IBOutlet UILabel *labApplyPersonPerformance;
@property (weak, nonatomic) IBOutlet UILabel *labApplyDate;
@property (weak, nonatomic) IBOutlet UILabel *labReturnCarDate;
@property (weak, nonatomic) IBOutlet UILabel *labSumPerformance;
@property (weak, nonatomic) IBOutlet UITextView *textVjcReasons;
@property (weak, nonatomic) IBOutlet UILabel *labjcApprovePersonName;
@property (weak, nonatomic) IBOutlet UITextView *textfRejectRsn;
@property (weak, nonatomic) IBOutlet UILabel *labApproveTime;

@end

@implementation BiBorrowManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBarTitle:@"借车审批"];
    [self setNavBackBtn];
    [self setNavBarWithHidden:NO];

    self.textfRejectRsn.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.textfRejectRsn.layer.borderWidth = 0.5;
    self.textVjcReasons.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.textVjcReasons.layer.borderWidth = 0.5;
    _ScrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, 600);
    [self dataFill];
    frame = _ScrollView.frame;
}

-(void)dataFill{
    if (self.borrowManageEntity) {
        _labBorrowCarPerson.text = self.borrowManageEntity.borrowCarPersonName;
        _labVIN.text = self.borrowManageEntity.vin;
        _labDirverName.text = self.borrowManageEntity.driverName;
        _labStoreroomManerName.text = self.borrowManageEntity.storeroomMangerName;
        _labDriverPreformance.text = self.borrowManageEntity.driverPreformance;
        _labStoreroomManerPreformance.text = self.borrowManageEntity.storeroomMangerPreformance;
        _labApplyPersonPerformance.text = self.borrowManageEntity.applyPersonPerformance;
        _labApplyDate.text = self.borrowManageEntity.applyDate;
        _labReturnCarDate.text = self.borrowManageEntity.returnCarDate;
        _labSumPerformance.text = self.borrowManageEntity.sumPerformance;
        _textVjcReasons.text = self.borrowManageEntity.jcResaon;
        _textfRejectRsn.text = self.borrowManageEntity.jcRejectReasons;
//       为空时取当前
        if (self.borrowManageEntity.jcApprovePersonName.length > 0) {
             _labjcApprovePersonName.text = self.borrowManageEntity.jcApprovePersonName;
        }else{
             _labjcApprovePersonName.text =  [BiUserEntity sharedInstance].name;
        }
       
        
        NSString * jcApproveDate = (self.borrowManageEntity.jcApproveDate.length==0)?[BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:mm"]:self.borrowManageEntity.jcApproveDate;
        
        _labApproveTime.text = jcApproveDate;
        
    }
}

- (IBAction)btnPermissionClick:(UIButton *)sender {

    NSString * jcApprovePersonId = nil;
    
    if (self.borrowManageEntity.jcApprovePersonId == 0) {
        jcApprovePersonId = [BiUserEntity sharedInstance].name;
    }else{
        jcApprovePersonId = self.borrowManageEntity.jcApprovePersonId;
    }
    
    NSDictionary *paraDic = [NSDictionary dictionaryWithObjectsAndKeys:
                            self.borrowManageEntity.e_id,@"id",
                             @"1",@"status",
                             @"2",@"jcStatus",
                             jcApprovePersonId,@"jcApprovePersonId",
                             [BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:mm"],@"jcApproveDate",
                             _textVjcReasons.text,@"jcRejectReasons"
                             ,nil];
    [[BiNetEngine shareInstance] requestBorrowCarCheck:self paraDict:paraDic];
    [self asyshowHUDView:WAITING CurrentView:self];
}

- (IBAction)btnRejectClick:(UIButton *)sender {

    NSDictionary *paraDic = [NSDictionary dictionaryWithObjectsAndKeys:
                             self.borrowManageEntity.e_id,@"id",
                             @"0",@"status",
                             @"2",@"jcStatus",
                             _textfRejectRsn.text,@"jcRejectReasons",
                             nil];
    //    NSLog(@"Reject%@",paraDic);
    [[BiNetEngine shareInstance] requestBorrowCarCheck:self paraDict:paraDic];
    [self asyshowHUDView:WAITING CurrentView:self];
}

#pragma mark -textview delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([@"\n" isEqualToString:text] == YES)
    {   
        [textView resignFirstResponder];
        
        
        return NO;
    }
    return YES;
}


- (void)textViewDidBeginEditing:(UITextView *)textView{
    frame= _ScrollView.frame;
    

    _ScrollView.frame = CGRectMake(0, _ScrollView.frame.origin.y - 200, _ScrollView.frame.size.width, _ScrollView.frame.size.height);
    
}


- (void)textViewDidEndEditing:(UITextView *)textView{
    
    [textView resignFirstResponder];
    
    _ScrollView.frame = frame;
    
}



#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    
    switch (tag) {
        case NET_GET_CAR_BORROW_CHECK:
        {
            [self.navigationController popViewControllerAnimated:YES];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
    
}

@end
